<template>
<h2>新增优惠券</h2>
<table>
    <tr>
        <td>优惠券名称</td>
        <td>
            <input type="text" placeholder="请输入优惠券名称" v-model="model.discountName"/>
        </td>
    </tr>
    <tr>
        <td>优惠券类型</td>
        <td>
            <select v-model="model.discountType">
                <option value="0">请选择</option>
                <option value="普通券">普通券</option>
                <option value="商品券">商品券</option>
                <option value="通用券">通用券</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>优惠券面值</td>
        <td>
            <input type="text" placeholder="请输入优惠券面值" v-model="model.discountZhi"/>
        </td>
    </tr>
    <tr>
        <td>优惠券最低消费</td>
        <td>
            <input type="text" placeholder="请输入优惠券最低消费" v-model="model.discountMin"/>
        </td>
    </tr>
    <tr>
        <td>优惠券有效时长</td>
        <td>
            <input type="text" placeholder="请输入优惠券有效时长" v-model="model.discountTime"/>天
        </td>
    </tr>
    <tr>
        <td>适用门店</td>
        <td>
            <select v-model="model.dShopid">
                <option value="0">请选择</option>
                <option v-for="item in shops" :value="item.shopid">{{ item.shopName }}</option>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" value="提交" class="btn btn-info" @click="Add"/>
        </td>
    </tr>
</table>
</template>
<script setup lang="ts">

import { ref,onMounted} from 'vue'
import axios from 'axios'
//优惠券实体
const model=ref({
    discountName:'',
    discountType:0,
    discountZhi:null,
    discountMin:null,
    discountTime:null,
    dShopid:0
})
//门店信息
const shops=ref([
    {
        shopid:0,
        shopName:''
    }
])
onMounted(()=>{
    axios.get('https://localhost:7000/api/Discount/GetShops')
    .then(res=>{
        shops.value=res.data
    })
    .catch(err=>{
        console.log(err);
    })
})


const Add=()=>{
    var obj={
        discountName:model.value.discountName,
        discountType:model.value.discountType,
        discountZhi:model.value.discountZhi,
        discountMin:model.value.discountMin,
        discountTime:model.value.discountTime,
        dShopid:model.value.dShopid
    }
    axios.post('https://localhost:7000/api/Discount/DiscountAdd',obj)
    .then(res=>{
        if(res.data==-1){
            alert('优惠券名称不能重复');
        }
        else if(res.data>0){
            alert('添加成功');
        }
        else{
            alert('添加失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}


</script>